/**
 * 
 */
package jvs.peer.sec;

import java.io.Serializable;

/**
 * @author qiangli
 * 
 */
public class Role implements Serializable {

	public static enum Type {
		affiliation("affiliation"), group("group"), nick("nick"), role("role"), uid("uid");
		
		protected String type;
		
		private Type(String type) {
			this.type = type;
		}

		@Override
		public String toString() {
			return type;
		}
		
	}

	private static final long serialVersionUID = 1L;

	public static Role.Type toType(String s) {
		if (s == null) return null;
		switch(s.charAt(0)) {
		case 'a': return Role.Type.affiliation;
		case 'r': return Role.Type.role;
		case 'g': return Role.Type.group;
		case 'u': return Role.Type.uid;
		case 'n': return Role.Type.nick;
		default: return null;
		}
	}
	private String name;

	private Type type;

	public Role() {
	}

	public Role(String type, String name) {
		this(toType(type),name);
	}

	public Role(Type type, String name) {

		this.type = type;
		this.name = name;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		final Role other = (Role) obj;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (type == null) {
			if (other.type != null)
				return false;
		} else if (!type.equals(other.type))
			return false;
		return true;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @return the type
	 */
	public Type getType() {
		return type;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result + ((type == null) ? 0 : type.hashCode());
		return result;
	}

	/**
	 * @param name
	 *            the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @param type
	 *            the type to set
	 */
	public void setType(Type type) {
		this.type = type;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "["+type+"] "+name;
	}
	
}
